package com.naiterui.ehp.bs.weixin.controller.inner;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.naiterui.ehp.bp.bo.subscribe.AdvisoryReminderBO;
import com.naiterui.ehp.bp.bo.subscribe.InquiryRemindBO;
import com.naiterui.ehp.bp.bo.subscribe.PrescriptionPayBO;
import com.naiterui.ehp.bp.bo.subscribe.ReceiptReminderBO;
import com.naiterui.ehp.bp.bo.subscribe.RefundNoticeBO;
import com.naiterui.ehp.bp.bo.subscribe.ServiceEvaluationBO;
import com.naiterui.ehp.bp.bo.subscribe.ShipNoticeBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.weixin.service.ISubscribeNoticeService;
import com.naiterui.ehp.bs.weixin.subscribe.AdvisoryReminderSubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.InquiryRemindSubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.PrescriptionPaySubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.ReceiptReminderSubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.RefundNoticeSubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.ServiceEvaluationSubscribe;
import com.naiterui.ehp.bs.weixin.subscribe.ShipNoticeSubscribe;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内部消息发送
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021/5/24 11:05
 **/
@Slf4j
@Api(tags = {SwaggerTag.WEIXIN + SwaggerTag.INNER_API + SwaggerTag.WECHAT_MESSAGE})
@RestController
@RequestMapping("weixin/inner/message")
@RequiredArgsConstructor
public class InnerMessageSendController {

    private static final int FAILURE = 1;
    private final ISubscribeNoticeService subscribeNoticeService;

    @ApiOperation("发送问诊提醒订阅消息")
    @PostMapping("/subscribe/send/inquiry/remind")
    public Integer subscribeSendInquiryRemind(@RequestBody InquiryRemindBO inquiryRemindBO) {
        String openId = inquiryRemindBO.getOpenId();
        String inquiryDoctor = inquiryRemindBO.getInquiryDoctor();
        String inquiryContent = inquiryRemindBO.getInquiryContent();
        String reminderContent = inquiryRemindBO.getReminderContent();
        String promptDesc = inquiryRemindBO.getPromptDesc();
        if (StrUtil.isAllBlank(openId, inquiryDoctor, inquiryContent, reminderContent, promptDesc)) {
            log.error("发送订阅消息错误, 参数为空, param: {}", JSONUtil.toJsonStr(inquiryRemindBO));
            return FAILURE;
        }
        InquiryRemindSubscribe inquiryRemindSubscrib = InquiryRemindSubscribe.builder()
                // 问诊医生中文名10个汉字内；纯英文名20个字母内；中文和字母混合按中文名算，10个字内
                .inquiryDoctor(StringUtil.isContainChinese(inquiryDoctor) ? StringUtil.maxLength(inquiryDoctor, 10)
                        : StringUtil.maxLength(inquiryDoctor, 20))
                // 问诊内容、提醒内容、提示内容20个以内字符
                .inquiryContent(StringUtil.maxLength(inquiryContent, 20))
                .reminderContent(StringUtil.maxLength(reminderContent, 20))
                .promptDesc(StringUtil.maxLength(promptDesc, 20))
                .build();
        inquiryRemindSubscrib.setOpenId(openId);
        inquiryRemindSubscrib.setTargetPage(inquiryRemindBO.getTargetPage());
        return this.subscribeNoticeService.notice(inquiryRemindSubscrib);
    }

    @ApiOperation("发送退款通知订阅消息")
    @PostMapping("/subscribe/send/refund/notice")
    public Integer subscribeSendRefundNotice(@RequestBody RefundNoticeBO refundNoticeBO) {
        String openId = refundNoticeBO.getOpenId();
        String orderNumber = refundNoticeBO.getOrderNumber();
        String productName = refundNoticeBO.getProductName();
        String refundAmount = refundNoticeBO.getRefundAmount();
        String refundCause = refundNoticeBO.getRefundCause();
        String remarks = refundNoticeBO.getRemarks();
        if (StrUtil.isAllBlank(openId, orderNumber, productName, refundAmount, refundCause, remarks)) {
            log.error("发送退款通知订阅消息, 参数为空, param: {}", JSONUtil.toJsonStr(refundNoticeBO));
            return FAILURE;
        }
        RefundNoticeSubscribe refundNoticeSubscribe = RefundNoticeSubscribe.builder()
                .orderNumber(StringUtil.maxLength(orderNumber, 32))
                .productName(StringUtil.maxLength(productName, 20))
                .refundAmount(refundAmount)
                .refundCause(StringUtil.maxLength(refundCause, 20))
                .remarks(StringUtil.maxLength(remarks, 20))
                .build();
        refundNoticeSubscribe.setOpenId(openId);
        refundNoticeSubscribe.setTargetPage(refundNoticeBO.getTargetPage());
        return this.subscribeNoticeService.notice(refundNoticeSubscribe);
    }

    @ApiOperation("发送服务评价通知订阅消息")
    @PostMapping("/subscribe/send/service/evaluation")
    public Integer subscribeSendServiceEvaluation(@RequestBody ServiceEvaluationBO serviceEvaluationBO) {
        ServiceEvaluationSubscribe serviceEvaluationSubscribe = ServiceEvaluationSubscribe.builder()
                .serviceItem(StringUtil.maxLength(serviceEvaluationBO.getServiceItem(), 20))
                .completeTime(serviceEvaluationBO.getCompleteTime())
                .tips(StringUtil.maxLength(serviceEvaluationBO.getTips(), 20))
                .build();
        serviceEvaluationSubscribe.setOpenId(serviceEvaluationBO.getOpenId());
        serviceEvaluationSubscribe.setTargetPage(serviceEvaluationBO.getTargetPage());
        return this.subscribeNoticeService.notice(serviceEvaluationSubscribe);
    }

    @ApiOperation("发送收货提醒通知订阅消息")
    @PostMapping("/subscribe/send/receipt/reminder")
    public Integer subscribeSendReceiptReminder(@RequestBody ReceiptReminderBO receiptReminderBO) {
        ReceiptReminderSubscribe receiptReminderSubscribe = ReceiptReminderSubscribe.builder()
                .orderNumber(StringUtil.maxLength(receiptReminderBO.getOrderNumber(), 32))
                .productName(StringUtil.maxLength(receiptReminderBO.getProductName(), 20))
                .confirmReceiptTime(receiptReminderBO.getConfirmReceiptTime())
                .tips(StringUtil.maxLength(receiptReminderBO.getTips(), 20))
                .build();
        receiptReminderSubscribe.setOpenId(receiptReminderBO.getOpenId());
        receiptReminderSubscribe.setTargetPage(receiptReminderBO.getTargetPage());
        return this.subscribeNoticeService.notice(receiptReminderSubscribe);
    }

    @ApiOperation("发送发货通知订阅消息")
    @PostMapping("/subscribe/send/ship/notice")
    public Integer subscribeSendShipNotice(@RequestBody ShipNoticeBO shipNoticeBO) {
        ShipNoticeSubscribe shipNoticeSubscribe = ShipNoticeSubscribe.builder()
                .orderNumber(StringUtil.maxLength(shipNoticeBO.getOrderNumber(), 32))
                .orderItem(StringUtil.maxLength(shipNoticeBO.getOrderItem(), 20))
                .expressDeliveryCompany(StringUtil.maxLength(shipNoticeBO.getExpressDeliveryCompany(), 20))
                .expressDeliveryNumber(StringUtil.maxLength(shipNoticeBO.getExpressDeliveryNumber(), 32))
                .remarks(StringUtil.maxLength(shipNoticeBO.getRemarks(), 20))
                .build();
        shipNoticeSubscribe.setOpenId(shipNoticeBO.getOpenId());
        shipNoticeSubscribe.setTargetPage(shipNoticeBO.getTargetPage());
        return this.subscribeNoticeService.notice(shipNoticeSubscribe);
    }

    @ApiOperation("发送咨询提醒订阅消息")
    @PostMapping("/subscribe/send/advisory/reminder")
    public Integer subscribeSendAdvisoryReminder(@RequestBody AdvisoryReminderBO advisoryReminderBO) {
        AdvisoryReminderSubscribe advisoryReminderSubscribe = AdvisoryReminderSubscribe.builder()
                .reminderContent(StringUtil.maxLength(advisoryReminderBO.getReminderContent(), 20))
                .appointment(advisoryReminderBO.getAppointment())
                .promptDesc(StringUtil.maxLength(advisoryReminderBO.getPromptDesc(), 20))
                .build();
        advisoryReminderSubscribe.setOpenId(advisoryReminderBO.getOpenId());
        advisoryReminderSubscribe.setTargetPage(advisoryReminderBO.getTargetPage());
        return this.subscribeNoticeService.notice(advisoryReminderSubscribe);
    }

    @ApiOperation("发送处方支付提醒订阅消息")
    @PostMapping("/subscribe/send/prescription/pay")
    public Integer subscribeSendPrescriptionPay(@RequestBody PrescriptionPayBO prescriptionPayBO) {
        PrescriptionPaySubscribe prescriptionPaySubscribe = PrescriptionPaySubscribe.builder()
            .inquirerName(StringUtil.maxLength(prescriptionPayBO.getInquirerName(), 20))
            .prescriptionTime(prescriptionPayBO.getPrescriptionTime())
            .remark(StringUtil.maxLength(prescriptionPayBO.getRemark(), 20)).build();
        prescriptionPaySubscribe.setOpenId(prescriptionPayBO.getOpenId());
        prescriptionPaySubscribe.setTargetPage(prescriptionPayBO.getTargetPage());
        return this.subscribeNoticeService.notice(prescriptionPaySubscribe);
    }
}
